package com.esp.soccertutor.ui.xmlparser;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esp.soccertutor.ui.bean.BeanPurchase;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.util.Log;
import com.esp.soccertutor.util.Utils;

public class XMLParser_MyPurchase {
	
	public String TAG = XMLParser_MyPurchase.class.getCanonicalName();
	public String strServerXML;
	public static Document docPurchaseList;
	public static int STATUS_CODE;
	private int RESPONSE_CODE = -1;
	private String MESSAGE;
	private int NODE_COUNT;
	private ArrayList<BeanPurchase> arryLstPurchase;
	
	public XMLParser_MyPurchase(String wsURL){
		
		try
		{
			XMLDocument.getHttpGetXML(wsURL);
			strServerXML = Utils.readTxt(Const.CONTEXT,"Purchase.txt");
			
			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docPurchaseList = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
		
	}
	/**
	 * THIS CALLBACK WHEN WE REACHED DOCUMENT
	 * @return
	 */
	public static Document GetDocument(){
		
		if(docPurchaseList != null){
			return docPurchaseList;
		}else{
			return null;
		}
	}
	
	public ArrayList<BeanPurchase> getPurchaseList(Document docCategory){
		try
		{
			if(docCategory != null)
				return docBuild(docCategory);
			else
				return null;
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	private ArrayList<BeanPurchase> docBuild(Document doc) throws Exception{
		
		if(doc != null)
		{

			Element elem$root = doc.getDocumentElement();
			NodeList nl$item = elem$root.getElementsByTagName("code");
			Element  line  = (Element)nl$item.item(0);
			RESPONSE_CODE = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line, "code"));
			
			NodeList nodeMessage = elem$root.getElementsByTagName("message");
			line  = (Element)nodeMessage.item(0);
			MESSAGE = XMLDocument.getCharacterDataFromElement(line, "message");
			
			NodeList nodeCount = elem$root.getElementsByTagName("count");
			line = (Element)nodeCount.item(0);
			NODE_COUNT = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line,"count"));
				
			
			NodeList nodeContent = elem$root.getElementsByTagName("content");
			
			if(RESPONSE_CODE == 1)
			{
				arryLstPurchase = new ArrayList<BeanPurchase>();
				
				for (int i = 0; i < nodeContent.getLength(); i++) 
					{
						
						BeanPurchase mBeanPurchase = new BeanPurchase();
						
						Element elementPos= (Element) nodeContent.item(i);
						
						NodeList nodeID = elementPos.getElementsByTagName("id");
						line  = (Element)nodeID.item(0);
						mBeanPurchase.setId(XMLDocument.getCharacterDataFromElement(line, "id"));
						
						NodeList nodeTitle = elementPos.getElementsByTagName("title");
						line  = (Element)nodeTitle.item(0);
						mBeanPurchase.setTitle(XMLDocument.getCharacterDataFromElement(line, "title"));
						
						NodeList nodeImageURL = elementPos.getElementsByTagName("imageURL");
						line  = (Element)nodeImageURL.item(0);
						mBeanPurchase.setImageURL(XMLDocument.getCharacterDataFromElement(line, "imageURL"));
						
						NodeList nodeDesc = elementPos.getElementsByTagName("desc");
						line  = (Element)nodeDesc.item(0);
						mBeanPurchase.setDesc(XMLDocument.getCharacterDataFromElement(line, "desc"));
						
						
						NodeList nodeFileName = elementPos.getElementsByTagName("filename");
						line  = (Element)nodeFileName.item(0);
						mBeanPurchase.setFilename(XMLDocument.getCharacterDataFromElement(line, "filename"));
						
						NodeList nodeIsDownloaded = elementPos.getElementsByTagName("isDonwloaded");
						line  = (Element)nodeIsDownloaded.item(0);
						boolean value = XMLDocument.getCharacterDataFromElement(line, "isDonwloaded").equals("No")
								? false : true;
						mBeanPurchase.setDownload(value);
						arryLstPurchase.add(mBeanPurchase);
					
					}
			}
			
		}
		return arryLstPurchase;
	}
}
